package com.feng.freader.view.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.CSJAdError;
import com.bytedance.sdk.openadsdk.CSJSplashAd;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.bytedance.sdk.openadsdk.mediation.MediationConstant;
import com.feng.freader.R;
import com.feng.freader.util.UIUtils;
import com.feng.freader.view.pintu.WelcomePage;


/**
 * 融合demo，开屏广告使用示例
 *
 * 注意：每次加载的广告，只能展示一次
 *
 * 接入步骤：
 * 1、创建AdSlot对象
 * 2、创建TTAdNative对象
 * 3、创建加载、展示监听器
 * 4、加载广告
 * 5、加载并渲染成功后，展示广告
 */
public class MediationSplashActivity extends Activity {

    private FrameLayout mSplashContainer;

    private TTAdNative.CSJSplashAdListener mCSJSplashAdListener;
    private CSJSplashAd.SplashAdListener mCSJSplashInteractionListener;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mediation_activity_splash);
        mSplashContainer = findViewById(R.id.fl_content);

        // 加载并展示广告
        loadAndShowSplashAd();
    }

    private void loadAndShowSplashAd() {
        // 创建开屏自定义兜底对象，例如穿山甲如下
//        MediationSplashRequestInfo csjSplashRequestInfo = new MediationSplashRequestInfo(
//                MediationConstant.ADN_PANGLE, // 穿山甲
//                "代码位Id", // adn开屏广告代码位Id，注意不是聚合广告位Id
//                "appId",   // adn应用id，注意要跟初始化传入的保持一致
//                "appKey"   // adn没有appKey时，传入空即可
//        ) {};

        /** 1、创建AdSlot对象 */
        AdSlot adSlot = new AdSlot.Builder()
//                .setCodeId("889310637")
                .setCodeId("889367465")
                .setImageAcceptedSize(UIUtils.getScreenWidthInPx(this),UIUtils.getScreenHeightInPx(this))
                .build();

        /** 2、创建TTAdNative对象 */
        TTAdNative adNativeLoader = TTAdSdk.getAdManager().createAdNative(this);

        /** 3、创建加载、展示监听器 */
        initListeners();

        /** 4、加载广告 */
        adNativeLoader.loadSplashAd(adSlot, mCSJSplashAdListener, 3500);
    }

    private void initListeners() {
        this.mCSJSplashAdListener = new TTAdNative.CSJSplashAdListener() {
            @Override
            public void onSplashRenderSuccess(CSJSplashAd csjSplashAd) {
                /** 5、渲染成功后，展示广告 */
                Log.d("ad_splash", "splash render success");
                csjSplashAd.setSplashAdListener(mCSJSplashInteractionListener);
                View splashView = csjSplashAd.getSplashView();
                UIUtils.removeFromParent(splashView);
                mSplashContainer.removeAllViews();
                mSplashContainer.addView(splashView);
            }

//            @Override
//            public void onSplashLoadSuccess() {
//                Log.d("ad_splash", "splash load success");
//            }

            @Override
            public void onSplashLoadSuccess(CSJSplashAd csjSplashAd) {

            }

            @Override
            public void onSplashLoadFail(CSJAdError csjAdError) {
                Log.d("ad_splash", "splash load fail, errCode: " + csjAdError.getCode() + ", errMsg: " + csjAdError.getMsg());
            }

            @Override
            public void onSplashRenderFail(CSJSplashAd csjSplashAd, CSJAdError csjAdError) {
                Log.d("ad_splash", "splash render fail, errCode: " + csjAdError.getCode() + ", errMsg: " + csjAdError.getMsg());
            }
        };
        this.mCSJSplashInteractionListener = new CSJSplashAd.SplashAdListener() {
            @Override
            public void onSplashAdShow(CSJSplashAd csjSplashAd) {
                Log.d("ad_splash", "splash show");
            }

            @Override
            public void onSplashAdClick(CSJSplashAd csjSplashAd) {
                Log.d("ad_splash", "splash click");
            }

            @Override
            public void onSplashAdClose(CSJSplashAd csjSplashAd, int i) {
                Log.d("ad_splash", "splash close");
                gotoMain();
                finish();
            }
        };
    }

    private void gotoMain() {
//        Intent intent = new Intent(this, MainActivity.class);
//        startActivity(intent);
        Intent intent32 = new Intent(this, WelcomePage.class);
        startActivity(intent32);
    }
}